package com.jiuyun.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiuyun.entity.JsonResult;
import com.jiuyun.entity.Users;
import com.jiuyun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  Spring + SpringMVC + Mybatis = SSM
 *  CrossOrigin 支持跨域
 */
@Controller
@CrossOrigin
public class UsersController {
    @Autowired
    UserService userService;

    @GetMapping("/users")
    public String index(ModelMap map ,int page){
        System.out.println("进入 UsersController控制器1...");
        PageHelper.startPage(page, 5);
        List list=  userService.getAll();

        PageInfo pinfo = new PageInfo(list);
        map.put("pinfo", pinfo );
        return "index";  // index.jsp
    }

    @PostMapping("/users")
    public String add(Users user){
        userService.add(user);
        return "redirect:users?page=1";
    }

    //今天组长新增的方法
    @GetMapping("/hello")
    public String hello(){
        System.out.println("组员加的功能");
        return "game";
    }

    @PutMapping("/users")
    @ResponseBody
    public JsonResult update(@RequestBody  Users user){
        userService.update(user);
        JsonResult ok = new JsonResult(200,"修改成功",null);
        return ok;
    }

    @DeleteMapping("/users/{uid}")
    @ResponseBody
    public JsonResult del(@PathVariable("uid") int userid){
        userService.del(userid);
        JsonResult ok = new JsonResult(200,"删除成功"+userid,null);
        return ok;
    }

    @GetMapping("/ajaxusers")
    @ResponseBody  // 把返回对象 变成JSON
    public JsonResult ajax( ){
        System.out.println("进入 UsersController控制器2...");
        List list=  userService.getAll();
        JsonResult ok = new JsonResult(200,"获取列表成功",list);
        return ok;
    }

    public String text(){
        return "index";
    }
    public String text1(){
        return "index";
    }

}
